package com.polaris.live.popup.userprofile

import android.content.Context
import android.view.View
import com.polaris.live.R
import com.polaris.live.common.cacha.UserManager
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.network.requestLoading
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.constant.LiveConst
import com.polaris.live.constant.UserConst
import com.polaris.live.databinding.PopupUserProfilePartyBinding
import com.polaris.live.interface_bag.PAGViewListener
import com.polaris.live.manager.LiveStateManager
import com.polaris.live.net.liveService
import com.polaris.live.popup.base.BaseLiveBottomPopupView
import com.polaris.live.resp.back_resp.InfoOtherResp
import com.polaris.live.resp.back_resp.toUserBasicVo
import com.polaris.live.resp.go_resp.LiveFollowBean
import com.polaris.live.ui.party.widget.seat.bean.BaseSeatBean
import com.polaris.live.utils.StartCallProxy
import com.polaris.live.utils.photo.PicturePreview
import com.polaris.live.utils.route.RoutePath
import com.polaris.live.utils.route.Router
import com.polaris.live.widget.pag.AddFollowView.Companion.LIVE_ADD_FOLLOWE_ALPHA
import org.libpag.PAGView

/**
 * PartyUserProfilePopup用户资料弹窗
 *
 * @author Created by 天晴 on 2024/2/29/027 17:44
 */
class PartyUserProfilePopup(
    context: Context,
    private val mViewModel: BaseViewModel,
    private val mInfoOtherResp: InfoOtherResp,
    private val mPage: Int,
    private val mObjectId: Long?,
    private val mAnchorId: Long?,
    private val mSeatBean: BaseSeatBean?,
    private val onMessageClickListener: OnClickListener? = null,
    private val onFollowerClickListener: (() -> Unit)? = null,
    private val onLeaveSeatClickListener: (() -> Unit)? = null,
    private val onGiftClickListener: ((infoOtherResp: InfoOtherResp) -> Unit)? = null,
    private val onSelectMoreCardListener: ((InfoOtherResp) -> Unit)? = null,
) : BaseLiveBottomPopupView<PopupUserProfilePartyBinding>(
    context,
    { PopupUserProfilePartyBinding.bind(it) }) {

    override fun getImplLayoutId(): Int {
        return R.layout.popup_user_profile_party
    }

    override fun onCreate() {
        super.onCreate()
        mBinding.apply {
            val goneCall = mAnchorId == mInfoOtherResp.userId
            baseView.initView(mInfoOtherResp, mPage, goneCall, {
                StartCallProxy.builder(this@PartyUserProfilePopup, context).startCall(
                    mViewModel,
                    UserConst.From.LIVE,
                    UserConst.LastFrom.LIVE_DETAIL,
                    LiveConst.SenderFrom.MANUAL,
                    mInfoOtherResp.userId,
                    mInfoOtherResp.toUserBasicVo(),
                    {
                        dismiss()
                    })
            }, { data, view, position ->
                PicturePreview.with(activity)
                    .byView(view)
                    .setImageUrlList(data)
                    .setIndex(position)
                    .setUserInfo(mInfoOtherResp)
                    .go()
            }, {
            }, {
                if (mInfoOtherResp.userId != UserManager.getUserId()) {
                    Router.instance.setPath(RoutePath.USER_SPACE_ACTIVITY)
                        .withInt("page", mPage)
                        .withAny("userBasic", mInfoOtherResp.toUserBasicVo())
                        .withLong("userId", mInfoOtherResp.userId).navigation()
                }

            })

            addFollower.apply {
                setPathType(LIVE_ADD_FOLLOWE_ALPHA)
                addListener(object : PAGViewListener {
                    override fun onAnimationEnd(p0: PAGView?) {
                        super.onAnimationEnd(p0)
                        addFollower.visibility = View.INVISIBLE
                    }
                })
                setRepeatCount(1)
            }

            if (mInfoOtherResp.relation?.followed == true) {
                addFollower.visibility = View.INVISIBLE
            }
            addFollower.setOnSingleClickListener {
                mObjectId?.let {
                    mViewModel.requestLoading(
                        { liveService.liveFollow(LiveFollowBean(it, mInfoOtherResp.userId)) },
                        {
                            onFollowerClickListener?.invoke()
                            mBinding.addFollower.play()
                            ToastGravity.showGravity(com.polaris.live.translation.R.string.follower_succeed)
                        })
                }
            }
            giftIt.setOnSingleClickListener {
                onGiftClickListener?.invoke(mInfoOtherResp)
                dismiss()
            }
            goMessage.setOnSingleClickListener {
                if (onMessageClickListener == null) {
                    Router.instance.setPath(RoutePath.MESSAGE_ACTIVITY)
                        .withInt("page", mPage)
                        .withString("nickname", mInfoOtherResp.nickname)
                        .withString("avatar", mInfoOtherResp.avatar)
                        .apply {
                            mInfoOtherResp.decoration?.avatarFrame?.also { af ->
                                withString("avatarFrame", af)
                            }
                            mInfoOtherResp.decoration?.chatBubble?.also { cb ->
                                withString("chatBubble", cb)
                            }
                        }
                        .withLong("userId", mInfoOtherResp.userId)
                        .navigation()
                } else {
                    onMessageClickListener.onClick(it)
                }
                dismiss()
            }

            icMoreCard.setOnSingleClickListener {
                onSelectMoreCardListener?.invoke(mInfoOtherResp)
                dismiss()
            }
            leaveSeat.setOnSingleClickListener {
                dismiss()
                onLeaveSeatClickListener?.invoke()
            }
            if (mInfoOtherResp.userId == UserManager.getUser().userId) {
                llBottom.visibility = View.VISIBLE
                goMessage.visibility = View.GONE
                addFollower.visibility = View.GONE
                val isVisible = if (mSeatBean?.userBasicVo?.userId == UserManager.getUserId()) View.VISIBLE else View.GONE
                leaveSeat.visibility = isVisible
                giftIt.visibility = isVisible
                icMoreCard.visibility = View.GONE
            } else if (onMessageClickListener == null) {
                llBottom.visibility = View.GONE
                leaveSeat.visibility = View.GONE
            }

            if (LiveStateManager.isCalling()) {
                llBottom.visibility = View.GONE
            }
        }

    }

    override fun getContentView(): View = mBinding.bottom
}